package com.yjhb.android.sbz.adapter;

import android.content.Context;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.RequestOptions;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.entity.CheckResult;
import com.yjhb.android.sbz.enums.TaskStatus;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.tool.ZxLogTool;
import com.jojo.android.zxlib.tool.ZxTimeTool;
import com.jojo.android.zxlib.view.CircleImageView;
import com.jojo.android.zxlib.view.checkbox.SmoothCheckBox;

import java.util.HashMap;
import java.util.Map;

/**
 * 创建者 zx
 * 创建时间 2020/12/15 15:44
 */
public class CheckListAdapter extends BaseRecyclerAdapter<CheckListAdapter.CheckListHolder, CheckResult> {
    private final String TAG = "CheckListAdapter";

    private boolean isEdit;
    /**
     * 选中的项
     */
    private Map<Long, CheckResult> selected = new HashMap<>();

    public CheckListAdapter(Context context) {
        super(context);
    }

    public void setEdit(boolean edit) {
        isEdit = edit;
    }

    /**
     * 选中所有
     *
     * @param b
     */
    public void selectedAll(boolean b) {
        try {
            if (isEdit && data != null) {
                selected.clear();
                if (b) {
                    for (CheckResult result : data) {
                        selected.put((long) result.getTaskOrder().getId(), result);
                    }
                }
                notifyDataSetChanged();
            }
        } catch (Exception e) {
            ZxLogTool.err(e);
        }

    }

    /**
     * 如果没有选中则选中这一项，否则取消选中
     *
     * @param index
     */
    public void check(int index) {
        try {
            if (isEdit && data != null) {
                long id = getItemId(index);
                CheckResult result = selected.get(id);
                if (result == null) {
                    selected.put(id, data.get(index));
                } else {
                    selected.remove(id);
                }
                notifyItemChanged(index);
            }
        } catch (Exception e) {
            ZxLogTool.err(e);
        }

    }

    public Map<Long, CheckResult> getSelectedMap() {
        return selected;
    }


    @Override
    protected CheckListHolder getHolder(View v) {
        return new CheckListHolder(v);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.item_task_check;
    }

    @Override
    public long getItemId(int position) {
        return data.get(position).getTaskOrder().getId();
    }

    @Override
    public void onBindViewHolder(@NonNull CheckListHolder checkListHolder, int i) {
        if (i == data.size() - 1 && i != 0) {
            checkListHolder.bg_layout.setBackgroundResource(R.drawable.corners_bottom_white);
            checkListHolder.line.setVisibility(View.GONE);
        } else if (i == 0 && data.size() == 1) {
            checkListHolder.bg_layout.setBackgroundResource(R.drawable.corners_white);
            checkListHolder.line.setVisibility(View.GONE);
        } else if (i == 0) {
            checkListHolder.bg_layout.setBackgroundResource(R.drawable.corners_top_white);
            checkListHolder.line.setVisibility(View.VISIBLE);
        } else {
            checkListHolder.bg_layout.setBackgroundResource(R.color.white);
            checkListHolder.line.setVisibility(View.VISIBLE);
        }

        Glide.with(context).load(entity.getAvatar())
                .apply(new RequestOptions().placeholder(R.mipmap.test_head))
                .into(checkListHolder.head_iv);
        checkListHolder.user_tv.setText(entity.getNickName());
        checkListHolder.user_id_tv.setText("ID:" + entity.getTaskOrder().getUserId());
        checkListHolder.submitDate_tv.setText("提交时间:" + ZxTimeTool.getTimeStr(entity.getTaskOrder().getSubmitDateTime()));

        final int index = i;
        checkListHolder.checkbox.setOnCheckedChangeListener(new SmoothCheckBox.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(SmoothCheckBox checkBox, boolean isChecked) {
                try {
                    long id = getItemId(index);
                    if (isChecked) {
                        selected.put(id, data.get(index));
                    } else {
                        selected.remove(id);
                    }
                    notifyItemChanged(index);
                } catch (Exception e) {
                    ZxLogTool.err(e);
                }
            }
        });
        String state = "";
        switch (TaskStatus.getStatus(entity.getTaskOrder().getState())) {
            case TASK_STATUS_SUBMIT:
                checkListHolder.failDetails_tv.setVisibility(View.GONE);
                checkListHolder.auditType_tv.setVisibility(View.GONE);
                checkListHolder.checkDate_tv.setVisibility(View.GONE);
                state = "未审核";
                if (isEdit) {
                    checkListHolder.checkbox.setVisibility(View.VISIBLE);
                    CheckResult result = selected.get(getItemId(i));
                    if (result == null) {
                        checkListHolder.checkbox.setChecked(false);
                    } else {
                        checkListHolder.checkbox.setChecked(true);
                    }
                } else {
                    checkListHolder.checkbox.setVisibility(View.GONE);
                }
                break;
            case TASK_STATUS_PASS:
                checkListHolder.failDetails_tv.setVisibility(View.GONE);
                checkListHolder.auditType_tv.setVisibility(View.VISIBLE);
                checkListHolder.checkbox.setVisibility(View.GONE);
                checkListHolder.checkDate_tv.setVisibility(View.VISIBLE);
                checkListHolder.checkDate_tv.setText("审核时间:" + ZxTimeTool.getTimeStr(entity.getTaskOrder().getAuditDateTime()));
                state = "已通过";
                break;
            case TASK_STATUS_FAIL:
                checkListHolder.failDetails_tv.setVisibility(View.VISIBLE);
                checkListHolder.auditType_tv.setVisibility(View.GONE);
                checkListHolder.checkbox.setVisibility(View.GONE);
                checkListHolder.checkDate_tv.setVisibility(View.VISIBLE);
                checkListHolder.checkDate_tv.setText("审核时间:" + ZxTimeTool.getTimeStr(entity.getTaskOrder().getAuditDateTime()));
                state = "不通过";
                checkListHolder.failDetails_tv.setOnClickListener(v -> {
                    if (itemClickListener!=null){
                        itemClickListener.onViewClick(v,"failDetails",index,data.get(index));
                    }
                });
                break;
            default:
                checkListHolder.auditType_tv.setVisibility(View.GONE);
                checkListHolder.checkbox.setVisibility(View.GONE);
                checkListHolder.checkDate_tv.setVisibility(View.GONE);
                checkListHolder.failDetails_tv.setVisibility(View.GONE);
        }
        checkListHolder.state_tv.setText(state);
        if (entity.getTaskOrder().getAuditType() == 0) {
            checkListHolder.auditType_tv.setText("人工审核");
        } else {
            checkListHolder.auditType_tv.setText("自动审核");
        }

    }


    class CheckListHolder extends RecyclerView.ViewHolder {
        CircleImageView head_iv;
        TextView user_tv;
        TextView submitDate_tv;
        TextView checkDate_tv;
        TextView state_tv;
        TextView user_id_tv;
        TextView auditType_tv;
        TextView failDetails_tv;
        SmoothCheckBox checkbox;
        View bg_layout;
        View line;

        public CheckListHolder(@NonNull View itemView) {
            super(itemView);
            head_iv = itemView.findViewById(R.id.head_iv);
            user_tv = itemView.findViewById(R.id.user_tv);
            user_id_tv = itemView.findViewById(R.id.user_id_tv);
            submitDate_tv = itemView.findViewById(R.id.submitDate_tv);
            checkDate_tv = itemView.findViewById(R.id.checkDate_tv);
            failDetails_tv = itemView.findViewById(R.id.failDetails_tv);
            state_tv = itemView.findViewById(R.id.state_tv);
            auditType_tv = itemView.findViewById(R.id.auditType_tv);
            bg_layout = itemView.findViewById(R.id.bg_layout);
            line = itemView.findViewById(R.id.line);
            checkbox = itemView.findViewById(R.id.checkbox);
        }
    }
}
